﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace Web.myadmin
{
    public partial class blog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SessionHelp.AssertLogin(this); //登录验证

            #region 显示正确的布局
            string cmd = Request.QueryString["cmd"];
            showTheRightRegion(cmd);
            switch (cmd)
            {
                case "add": initAddForm();break;
                case "edit":initEditFrom();break;
                case "view": initViewTable(); break;
                case "delete": delete(); break;
                default: showInfoList(); break;
            }
            #endregion
        }

        #region 信息列表和查询
        /// <summary>
        /// 查询数据
        /// </summary>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            showInfoList();
        }

        /// <summary>
        /// 数据列表
        /// </summary>
        protected void showInfoList()
        {
            #region 变量定义
            DataSet dset = new DataSet();
            BZZ.DAL.blog dal = new BZZ.DAL.blog();
            System.Collections.Generic.List<System.Data.SqlClient.SqlParameter> paraList = new List<System.Data.SqlClient.SqlParameter>();
            string dataFields = ""; //读取的字段名，空为所有
            string strWhere = "1=1"; //查询子句
            string strOrderBy = " id desc"; //排序参数，如果需要分页，则必填 
            #endregion

            #region 查询条件
            //string strId = txtQryId.Text.Trim();
            //if (strId != "" && strId != null )
            //{
            //    strWhere += " and id=@id";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@id", strId));
            //} 
            string strCataid = txtQryCataid.Text.Trim();
            if (strCataid != null && strCataid != "" )
            {
                strWhere += " and cataid=@cataid";
                paraList.Add(new System.Data.SqlClient.SqlParameter("@cataid", strCataid));
            } 
            string strSubcataid = txtQrySubcataid.Text.Trim();
            if (strSubcataid != null && strSubcataid != "" )
            {
                strWhere += " and subcataid=@subcataid";
                paraList.Add(new System.Data.SqlClient.SqlParameter("@subcataid", strSubcataid));
            } 
            //string strTag = txtQryTag.Text.Trim();
            //if (strTag != "" && strTag != null )
            //{
            //    strWhere += " and tag=@tag";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@tag", strTag));
            //} 
            //string strTitle = txtQryTitle.Text.Trim();
            //if (strTitle != "" && strTitle != null )
            //{
            //    strWhere += " and title=@title";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@title", strTitle));
            //} 
            //string strContent = txtQryContent.Text.Trim();
            //if (strContent != "" && strContent != null )
            //{
            //    strWhere += " and content=@content";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@content", strContent));
            //} 
            //string strComment = txtQryComment.Text.Trim();
            //if (strComment != "" && strComment != null )
            //{
            //    strWhere += " and comment=@comment";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@comment", strComment));
            //} 
            //string strHits = txtQryHits.Text.Trim();
            //if (strHits != "" && strHits != null )
            //{
            //    strWhere += " and hits=@hits";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@hits", strHits));
            //} 
            //string strCommentCount = txtQryCommentCount.Text.Trim();
            //if (strCommentCount != "" && strCommentCount != null )
            //{
            //    strWhere += " and commentCount=@commentCount";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@commentCount", strCommentCount));
            //} 
            //string strSourceId = txtQrySourceId.Text.Trim();
            //if (strSourceId != "" && strSourceId != null )
            //{
            //    strWhere += " and sourceId=@sourceId";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@sourceId", strSourceId));
            //} 
            //string strSourceUrl = txtQrySourceUrl.Text.Trim();
            //if (strSourceUrl != "" && strSourceUrl != null )
            //{
            //    strWhere += " and sourceUrl=@sourceUrl";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@sourceUrl", strSourceUrl));
            //} 
            //string strPubDate = txtQryPubDate.Text.Trim();
            //if (strPubDate != "" && strPubDate != null )
            //{
            //    strWhere += " and pubDate=@pubDate";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@pubDate", strPubDate));
            //} 
            //string strStatus = txtQryStatus.Text.Trim();
            //if (strStatus != "" && strStatus != null )
            //{
            //    strWhere += " and status=@status";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@status", strStatus));
            //} 
            //string strExt1 = txtQryExt1.Text.Trim();
            //if (strExt1 != "" && strExt1 != null )
            //{
            //    strWhere += " and ext1=@ext1";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@ext1", strExt1));
            //} 
            //string strExt2 = txtQryExt2.Text.Trim();
            //if (strExt2 != "" && strExt2 != null )
            //{
            //    strWhere += " and ext2=@ext2";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@ext2", strExt2));
            //} 
            #endregion

             #region 分页控件绑定
             int pageSize = 30; //分页每页记录个数
             int totalRecourd = 0; //总记录个数
             int currentPage = 1; //当前页码
             try { currentPage = int.Parse(Request.QueryString["p"]); }
             catch { }

             totalRecourd = dal.GetCount(strWhere, paraList);
             dset = dal.GetDatasetByPageSql(currentPage, pageSize, strWhere, strOrderBy, paraList, dataFields);
             if (dset!=null && dset.Tables[0].Rows.Count > 0)
             {
                 rptInfoList.DataSource = dset;
                 rptInfoList.DataBind();
                 litPager.Text = LIB.pager.getPageHtml(this, totalRecourd, pageSize);
             }
             else
             {
                 if (paraList.Count > 0)
                     litPromptMessage.Text = "没有查询到相关记录 ";
                 else
                 {
                     litPromptMessage.Text = " <a href='?cmd=add'>添加新纪录</a>";
                     divQuery.Visible = false;
                 }
             } 
             #endregion
             #region 单页数据绑定
             //strWhere += " " + strOrderBy;
             //dset = dal.GetDataset(strWhere, paraList, dataFields);
             //if (dset.Tables[0].Rows.Count > 0)
             //{
             //    rptInfoList.DataSource = dset;
             //    rptInfoList.DataBind();
             //}
             //else
             //{
             //    litPromptMessage.Text = "没有查询到相关记录";
             //} 
             #endregion

        }
        #endregion

        #region 添加、修改、删除操作
        /// <summary>
        /// 添加修改记录
        /// </summary>
        protected void btnSumit_Click(object sender, EventArgs e)
        {
            #region 获取数据
            bool isAdd = false;
            int varId ;	//id
            int varCataid ;	//一级分类
            int varSubcataid ;	//二级分类
            string varTag;	//标签
            string varTitle;	//标题
            string varContent;	//内容
            string varComment;	//点评
            int varHits ;	//点击数
            int varCommentCount ;	//评论数
            int varSourceId ;	//采集源id
            string varSourceUrl;	//来源网址
            DateTime varPubDate ;	//发布时间
            int varStatus ;	//状态
            string varExt1;	//扩展1
            string varExt2;	//扩展2
            try
            {
               varId =int.Parse(txtId.Text.Trim());	//id
               varCataid =int.Parse(ddlPid.SelectedValue);	//一级分类
               varSubcataid =int.Parse(txtSubcataid.Text.Trim());	//二级分类
               varTag = "";// txtTag.Text.Trim();	//标签
               varTitle =txtTitle.Text.Trim();	//标题
               varContent =txtContent.Text.Trim();	//内容
               varComment = "";// txtComment.Text.Trim();	//点评
               varHits = 0;// int.Parse(txtHits.Text.Trim());	//点击数
               varCommentCount = 0;// int.Parse(txtCommentCount.Text.Trim());	//评论数
               varSourceId = 0;// int.Parse(txtSourceId.Text.Trim());	//采集源id
               varSourceUrl = "";// txtSourceUrl.Text.Trim();	//来源网址
               varPubDate =DateTime.Parse(txtPubDate.Text.Trim());	//发布时间
               varStatus =int.Parse(txtStatus.Text.Trim());	//状态
               varExt1 = "";// txtExt1.Text.Trim();	//扩展1
               varExt2 = "";// txtExt2.Text.Trim();	//扩展2
            }
            catch
            {
               LIB.JsWrite.Alert(this, "输入数据格式不合法，请检查输入数据。");
               return;
            }
            if (varId == 0) isAdd = true;
            #endregion

            #region 赋值model
            BZZ.DAL.blog dal = new BZZ.DAL.blog();
            BZZ.Model.blog model = new BZZ.Model.blog();
            if (isAdd)
            {
                model.cataid = varCataid;	//一级分类
                model.subcataid = varSubcataid;	//二级分类
                model.tag = varTag;	//标签
                model.title = varTitle;	//标题
                model.content = varContent;	//内容
                model.comment = varComment;	//点评
                model.hits = varHits;	//点击数
                model.commentCount = varCommentCount;	//评论数
                model.sourceId = varSourceId;	//采集源id
                model.sourceUrl = varSourceUrl;	//来源网址
                model.pubDate = DateTime.Now;
                model.status = 1;
                model.ext1 = varExt1;	//扩展1
                model.ext2 = varExt2;	//扩展2
                if (dal.Add(model) > 0)
                {
                    if (ckSendMail.Checked) LIB.MailSender.Send("40061052@qzone.qq.com", model.title, model.content);
                    LIB.JsWrite.AlertAndGo(this, "添加成功", "?cmd=list");
                }
                else
                {
                    LIB.JsWrite.AlertAndGoback(this,"操作失败");
                }
            }
            else
            {
                model = dal.GetModel(varId);
                //model.id = varId;	//id
                model.cataid = varCataid;	//一级分类
                model.subcataid = varSubcataid;	//二级分类
                model.tag = varTag;	//标签
                model.title = varTitle;	//标题
                model.content = varContent;	//内容
                model.comment = varComment;	//点评
                //model.hits = varHits;	//点击数
                //model.commentCount = varCommentCount;	//评论数
                model.sourceId = varSourceId;	//采集源id
                model.sourceUrl = varSourceUrl;	//来源网址
                model.pubDate = varPubDate;	//发布时间
                model.status = varStatus;	//状态
                //model.ext1 = varExt1;	//扩展1
                //model.ext2 = varExt2;	//扩展2
                if (dal.Update(model) > 0)
                {
                    if (ckSendMail.Checked) LIB.MailSender.Send("40061052@qzone.qq.com", model.title, model.content);
                    LIB.JsWrite.Alert(this, "修改成功");
                }
                else
                {
                    LIB.JsWrite.AlertAndGoback(this,"操作失败");
                }
            }
            #endregion

           
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        protected void delete()
        {
            #region 删除操作
            int id = 0;
            try { id = int.Parse(Request.QueryString["id"]); }
            catch
            {
                LIB.JsWrite.Alert(this, "非法的参数");
                return;
            }

            BZZ.DAL.blog dal = new BZZ.DAL.blog();
            if (dal.Delete(id)>0)
            {
                LIB.JsWrite.AlertAndGo(this, "删除成功", "?cmd=list");
            }
            else
            {
                LIB.JsWrite.AlertAndGo(this, "操作失败", "?cmd=list");
            } 
            #endregion
        }
        #endregion

        #region 初始化添加表单
        /// <summary>
        /// 初始化添加表单
        /// </summary>
        protected void initAddForm()
        {
            lblFormTitle.Text = "添加信息";
            if (!IsPostBack)
            {

                //txtCataid.Text = "0";
                BindPid();
                txtSubcataid.Text = "0";
                //txtTag.Text = "";
                txtTitle.Text = "";
                txtContent.Text = "";
                //txtComment.Text = "";
                //txtHits.Text = "";
                //txtCommentCount.Text = "";
                //txtSourceId.Text = "";
                //txtSourceUrl.Text = "";
                txtPubDate.Text = DateTime.Today.ToString();
                txtStatus.Text = "1";
                //txtExt1.Text = "";
                //txtExt2.Text = "";
            }
        }
        #endregion

        #region 初始化修改表单
        /// <summary>
        /// 初始化修改表单
        /// </summary>
        protected void initEditFrom()
        {
            int id =Convert.ToInt32(Request.QueryString["id"]);
            lblFormTitle.Text = "修改信息";
            if (!IsPostBack)
            {
                BZZ.Model.blog model = new BZZ.Model.blog();
                BZZ.DAL.blog dal = new BZZ.DAL.blog();
                model = dal.GetModel(id);
                BindPid();
                txtId.Text = model.id.ToString();
                //txtCataid.Text = model.cataid.ToString();
                ddlPid.SelectedValue = model.cataid.ToString();
                txtSubcataid.Text = model.subcataid.ToString();
                //txtTag.Text = model.tag.ToString();
                txtTitle.Text = model.title.ToString();
                txtContent.Text = model.content.ToString();
                //txtComment.Text = model.comment.ToString();
                //txtHits.Text = model.hits.ToString();
                //txtCommentCount.Text = model.commentCount.ToString();
                //txtSourceId.Text = model.sourceId.ToString();
                //txtSourceUrl.Text = model.sourceUrl.ToString();
                txtPubDate.Text = model.pubDate.ToString();
                txtStatus.Text = model.status.ToString();
                //txtExt1.Text = model.ext1.ToString();
                //txtExt2.Text = model.ext2.ToString();
            }
        }
        #endregion

        #region 初始化查看表格
        /// <summary>
        /// 初始化查看表格
        /// </summary>
        protected void initViewTable()
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            lblViewTitle.Text = "查看信息";
            if (!IsPostBack)
            {
                BZZ.Model.blog model = new BZZ.Model.blog();
                BZZ.DAL.blog dal = new BZZ.DAL.blog();
                model = dal.GetModel(id);

                lblCataid.Text = model.cataid.ToString();
                lblSubcataid.Text = model.subcataid.ToString();
                lblTag.Text = model.tag.ToString();
                lblTitle.Text = model.title.ToString();
                lblContent.Text = model.content.ToString();
                lblComment.Text = model.comment.ToString();
                lblHits.Text = model.hits.ToString();
                lblCommentCount.Text = model.commentCount.ToString();
                lblSourceId.Text = model.sourceId.ToString();
                lblSourceUrl.Text = model.sourceUrl.ToString();
                lblPubDate.Text = model.pubDate.ToString();
                lblStatus.Text = model.status.ToString();
                lblExt1.Text = model.ext1.ToString();
                lblExt2.Text = model.ext2.ToString();
            }
        }

        #endregion

        #region 自定义函数

        protected void BindPid()
        {
            BZZ.DAL.cata dal = new BZZ.DAL.cata();
            DataSet dset = dal.GetDataset(" pid=0 ", null, "title as datatext,id as datavalue");
            ddlPid.DataSource = dset;
            ddlPid.DataBind();
            ddlPid.Items.Insert(0, new ListItem("顶级分类", "0"));

        }


        public string getCataNameByID(int id)
        {
            BZZ.DAL.cata dal = new BZZ.DAL.cata();
            return id > 0 ? dal.GetModel(id).title : "-";
        }
 



        #endregion


        #region 公共函数，不需要修改

        /// <summary>
        /// 根据cmd参数判断应该显示哪些部分内容
        /// </summary>
        /// <param name="cmd"></param>
        protected void showTheRightRegion(string cmd)
        {
            #region show or hide div
            switch (cmd)
            {
                case "add":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "edit":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "view":
                    divView.Visible = true;
                    divForm.Visible = false;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "delete":
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    break;
                case "query":
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    divQuery.Visible = true;
                    break;
                default:
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    divQuery.Visible = true;
                    break;
            }
            #endregion
        }
        #endregion



    }


}

